
import {
  createTaobaoGift,
  getGoodsDetail,
  getGoodsList,
  getOrderList,
  getRecordList,
} from '../entities/mall'
import { MallAdapter } from '../adapters/mall'
import { AppError } from '../../utils/error'
import { AppErrorCode } from '../../helper/constants'
import { getStore, setStore } from '../entities/store'

export class MallModel {
  isMock = false
  // 获取商品列表
  async getGoodsList(params) {
    const resp = await getGoodsList(params)
    if (resp.isSuccess) {
      return MallAdapter.fromAPIGoodsToGoods(resp.data)
    }
    throw new Error(resp.message)
  }
  // 获取商品详情
  async getGoodsDetail(params) {
    const resp = await getGoodsDetail(params)
    if (resp.isSuccess) {
      return MallAdapter.fromAPIGoodsDetailToDetail(resp.data)
    }
    throw new Error(resp.message)
  }
  // 获取订单列表 todo暂无此功能
  async getOrderList(params) {
    const resp = await getOrderList(params)
    if (resp.isSuccess) {
      return resp.data
    }
    throw new Error(resp.message)
  }
  // 获取购物金记录列表
  async getRecordList(params) {
    const resp = await getRecordList(params)
    if (resp.isSuccess) {
      return MallAdapter.fromAPIRecordsToRecords(resp.data)
    }
    throw new Error(resp.message)
  }
  // 创建淘礼金
  async createTaobaoGift(params) {
    const resp = await createTaobaoGift(params)
    if (resp.isSuccess) {
      return resp.data
    }
    let code = AppErrorCode.Unknown
    if (resp?.message?.includes('余额不足')) {
      code = AppErrorCode.NotSufficientFunds
    }
    throw new AppError(resp.message, code, resp.data)
  }

  async isAcceptPrivacy() {
    return await getStore('privacy')
  }
  async setAcceptPrivacy(accept) {
    await setStore('privacy', accept)
  }
}